//
//  MSNewTextField.m
//  MSJD
//
//  Created by 箭军刘 on 2018/10/30.
//  Copyright © 2018年 xiangbo. All rights reserved.
//

#import "MSNewTextField.h"

@interface MSNewTextField()

@property (weak, nonatomic) IBOutlet UIImageView *textIcon; // 输入框图片

@property (weak, nonatomic) IBOutlet UIView *codeView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *codeViewWidth;

@property (nonatomic, assign) MSTextType type;

@end

@implementation MSNewTextField

+ (instancetype)loadNewTextFieldWithType:(MSTextType)type {
    MSNewTextField *newText = [[[NSBundle mainBundle] loadNibNamed:@"MSNewTextField" owner:nil options:nil] lastObject];
    newText.type = type;
    newText.layer.cornerRadius = 25.0;
    [newText initSubViews];
    return newText;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        
        // 修改光标的颜色
        self.textField.tintColor = [UIColor colorWithHexString:@"#F96332"];
        // 修改输入框的默认字体颜色
        [self.textField setValue:[UIColor colorWithHexString:@"#BBBBBB"] forKeyPath:@"_placeholderLabel.textColor"];
        // 圆角
        self.layer.cornerRadius = 25;
    }
    return self;
}

#pragma mark - Init Method

- (void)initSubViews {
    if (self.type != MSTextTypeCode) {
        self.codeView.hidden = YES;
        self.codeViewWidth.constant = 0;
    }
    // 默认提示
    NSString *placeHolder = @"";
    NSString *imageName = @"";
    switch (self.type) {
        case MSTextTypePhone:
        {
            placeHolder = @"请输入手机号码";
            imageName = @"login_phone";
        }
            break;
        case MSTextTypePwd:
        {
            placeHolder = @"请输入密码";
            imageName = @"login_pwd";
            // 密文
            self.textField.secureTextEntry = YES;
        }
            break;
        case MSTextTypeCode:
        {
            placeHolder = @"请输入验证码";
            imageName = @"login_code";
            // 只允许输入数字
            self.textField.keyboardType = UIKeyboardTypeNumberPad;
        }
            break;
        default:
        {
            placeHolder = @"";
            imageName = @"login_phone";
        }
            break;
    }
    self.textField.placeholder = placeHolder;
    [self.textIcon setImage:[UIImage imageNamed:imageName]];
}

#pragma mark - XIB Response
// 验证码按钮点击
- (IBAction)codeButtonClick:(UIButton *)sender {
    if (self.CodeButtonClickBlock) {
        self.CodeButtonClickBlock(sender);
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
